<template>
  <base-breadcrumb :title="$route.meta.title">
    <!-- BIM考核记录 -->
    <div id="base-breadcrumb" style="position: relative; overflow: hidden">
      <!-- 搜索 -->
      <searchData :search-data="searchArr" @search="search" />
      <!-- 表格 -->
      <a-card :bordered="false">
        <!-- 表格 -->
        <list-table
          ref="table"
          :page-keys="['pageNum', 'pageSize', 'total']"
          :columns="columns"
          :start-index="0"
          :filter-data="filterData"
          :request="loadData"
          :export-permission="{ select: 'BIM_007_exp_05', all: 'BIM_007_exp_05' }"
          :export-config="{ use: true, select: $route.meta.title, all: $route.meta.title }"
        >
          <!-- 新增 -->
          <a-button
            slot="prefix"
            v-btnPermission="'BIM_007_add_01'"
            class="mb12"
            icon="plus"
            type="primary"
            @click="handleManage(2)"
          >
            新增
          </a-button>
          <!-- 下载附件 -->
          <a-button
            slot="suffix"
            v-btnPermission="'BIM_007_dow_06'"
            slot-scope="scoped"
            :loading="downLoading"
            class="mb12"
            icon="cloud-download"
            :disabled="scoped.selectedRowKeys && scoped.selectedRowKeys.length === 0"
            type="primary"
            @click="downLoadZip(scoped.selectedRowsMap)"
          >
            批量下载附件</a-button
          >
          <!-- 平均分 -->
          <template slot="averageScore" slot-scope="text, record">
            <a
              @click="
                editModel = 0
                selectRow = record
                visible = true
              "
              >{{ text }}</a
            >
          </template>
          <!-- 操作栏 -->
          <template slot="actions" slot-scope="text, record">
            <MoreButton>
              <template v-if="record.fileInfoList && record.fileInfoList.length > 0">
                <a
                  v-btnPermission="'BIM_007_loo_02'"
                  @click="
                    editModel = 3
                    fileList = record.fileInfoList
                    visible = true
                  "
                  >查看附件({{ record.fileInfoList ? record.fileInfoList.length : 0 }})</a
                >
                <!-- <a
                v-btnPermission="'BIM_007_loo_02'"
                @click="handleManage(0, record)"
              >查看</a> -->
              </template>
              <a v-btnPermission="'BIM_007_edi_03'" @click="handleManage(1, record)">编辑</a>
              <template>
                <a v-btnPermission="'BIM_007_del_04'" @click="handleRemove(record.id, search,'删除此数据不可进行恢复，请确认是否删除？')">删除</a>
              </template>
            </MoreButton>
          </template>
        </list-table>
      </a-card>
    </div>
    <!-- 查看附件 -->
    <DialogForm
      ref="dialogForm"
      v-if="visible"
      :visible.sync="visible"
      edit-model="V"
      :headerTitle="editModel !== 0 ? '查看附件' : '查看分数'"
      :width="1000"
      :fileList="editModel !== 0 ? fileList : []"
    >
      <template v-if="editModel === 0" slot="formHeader">
        <searchData :search-data="searchArrTwo" @search="searchDialog" />
        <!-- 考核得分 -->
        <b-table
          ref="tableDialogForm"
          :pageKeys="['pageNum', 'pageSize', 'total']"
          :scroll="{ y: 450 }"
          :columns="columnsTwo"
          :request="loadDataTwo"
        ></b-table>
      </template>
    </DialogForm>
  </base-breadcrumb>
</template>

<script>
import {
  getPageBimInspectRecord,
  getPageBimInspectRecordUnit,
  getPageTypesLabel,
  deleteBimInspectRecord
} from '@/api/bimIntegrate/bimResource.js'
import BasePage from '@/views/basePage'
import Selector from '@/components/Select/Selector'
import DialogForm from '@/components/DialogForm' // 弹窗

const columns = [
  {
    title: '序号',
    dataIndex: 'index',
    width: 80,
    customRender() {
      return Number(arguments[2]) + 1
    }
  },
  {
    title: '考核分类',
    ellipsis: true,
    dataIndex: 'inspectType',
    customRender() {
      return ['信息化', 'BIM'][arguments[0]] || '无'
    }
  },
  {
    title: '考核名称',
    ellipsis: true,
    dataIndex: 'inspectName'
  },
  {
    title: '考核层级',
    ellipsis: true,
    dataIndex: 'inspectLevel',
    customRender() {
      return ['分公司', '项目'][arguments[0]] || '无'
    }
  },
  {
    title: '考核月度',
    ellipsis: true,
    dataIndex: 'inspectMonth'
  },
  {
    title: '考核期数',
    ellipsis: true,
    dataIndex: 'inspectPeriod',
    customRender(e) {
      return e + '期'
    }
  },
  {
    title: '平均得分',
    align: 'center',
    dataIndex: 'averageScore',
    scopedSlots: { customRender: 'averageScore' }
  },
  {
    title: '更新时间',
    width: 180,
    ellipsis: true,
    dataIndex: 'modifiedTime'
  },
  {
    title: '操作',
    dataIndex: 'actions',
    width: '230px',
    align: 'center',
    fixed: 'right',
    scopedSlots: { customRender: 'actions' }
  }
]
export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    DialogForm
  },
  data() {
    this.columns = columns
    return {
      editModel: 0, // 操作类型：0 查看得分；1 编辑；2 新增；3 查看附件
      queryParam: {}, // 查询参数
      queryParamTwo: {}, // 得分列表查询参数
      inspectLevel: '', // 考核层级
      visible: false, // 查看附件弹窗控件
      fileList: [], // 当前文件列表
      selectRow: {}, // 选中项
      removeApi: { remove: deleteBimInspectRecord }, // 删除台账
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, { param: this.queryParam })
        return getPageBimInspectRecord(requestParameters).then((res) => {
          if (res.code === '0') {
            return { code: 200, data: res.data?.list, total: res.data?.total }
          }
        })
      },
      loadDataTwo: (parameter) => {
        const requestParameters = Object.assign({}, parameter, {
          param: { ...this.queryParamTwo, bimInspectRecordId: this.selectRow.id }
        })
        return getPageBimInspectRecordUnit(requestParameters).then((res) => {
          if (res.code === '0') {
            return { code: 200, data: res.data?.list, total: res.data?.total }
          }
        })
      }
    }
  },
  watch: {
    // 监听姓名更新得分表格
    'selectRow.id'(e) {
      e && this.$refs.tableDialogForm?.getData(null, true)
    }
  },
  computed: {
    // 得分表头
    columnsTwo() {
      let columns = [
        {
          title: '被考核单位',
          ellipsis: true,
          dataIndex: 'unitName'
        },
        {
          title: '是否为重点项目',
          isHidden: true,
          ellipsis: true,
          dataIndex: 'keyProject',
          customRender(e) {
            return ['否', '是'][e]
          }
        },
        {
          title: '得分',
          ellipsis: true,
          dataIndex: 'score'
        }
      ]
      return this.selectRow.inspectLevel === 0 ? columns.filter((item) => !item.isHidden) : columns
    },
    // 得分搜索配置项
    searchArrTwo() {
      let search = [
        {
          name: '被考核单位',
          type: 'input',
          value: 'unitName'
        },
        {
          name: '是否为重点项目',
          isHidden: true,
          type: 'select',
          value: 'keyProject',
          data: [
            {
              label: '否',
              value: 0
            },
            {
              label: '是',
              value: 1
            }
          ]
        }
      ]
      return this.selectRow.inspectLevel === 0 ? search.filter((item) => !item.isHidden) : search
    },
    // 列表搜索配置项
    searchArr() {
      let that = this
      return [
        // 搜索框
        {
          name: '考核名称',
          valueKey: 'inspectNameId',
          type: Selector,
          customApi: getPageTypesLabel,
          customQuery: { param: { type: '32' } },
          dataValue: 'data.list'
        },
        {
          name: '考核分类',
          type: 'select',
          value: 'inspectType',
          data: [
            {
              label: '信息化',
              value: 0
            },
            {
              label: 'BIM',
              value: 1
            }
          ]
        },
        {
          name: '考核层级',
          type: 'select',
          value: 'inspectLevel',
          data: [
            {
              label: '分公司级',
              value: 0
            },
            {
              label: '项目级',
              value: 1
            }
          ],
          // listeners: {
          //   change(e) {
          //     that.inspectLevel = e
          //   }
          // }
        },
        {
          name: '考核月度',
          type: 'month',
          value: 'inspectMonth'
        }
      ]
    }
  },
  methods: {
    downLoadZip(selectedRowsMap) {
      const arr = selectedRowsMap.get(1);
      let num = 0
      arr.forEach(item=>{
        num+= item.fileInfoList ? item.fileInfoList.length : 0
      })
      if(num==0) {
        this.$message.warning("当前选择无附件下载")
        return
      }
      this.downZip(selectedRowsMap)
    },
    /**
     * 格式话数据
     */
    filterData(datas) {
      let list = this.$clone(datas)
      list.forEach((item, index) => {
        item.index = index + 1
        item.inspectType = ['信息化', 'BIM'][Number(item.inspectType)] || ''
        item.inspectLevel = ['分公司', '项目'][Number(item.inspectLevel)] || ''
      })
      return list
    },
    /**
     * 搜索
     */
    search(value) {
      value && (this.queryParam = value)
      this.$refs.table.getData(null, true)
    },
    /**
     * 得分查询搜索
     */
    searchDialog(value) {
      value && (this.queryParamTwo = value)
      this.$refs.tableDialogForm.getData(null, true)
    },
    /**
     * 操作栏
     */
    handleManage(type, record) {
      this.editModel = type
      let query = {
        editModel: type
      }
      if (record && record.id) {
        query.id = record.id
      }
      this.$router.push({
        name: '/bimResource/assessmentRecordEdit',
        query
      })
    }
  }
}
</script>
